#!/usr/bin/python3
import requests, json

# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(
    requests.packages.urllib3.exceptions.InsecureRequestWarning
)

class hasTasksRequest(requests.Session):
    def __init__(self, host):
        super().__init__()
        self.host = host
        
    def do_get(self, url="", headers=None, params=None, data=None):
        url = self.host + url
        try:
            r = self.get(url, headers=headers, params=params, data=data, verify=False)
            return r.json() if r and r.status_code == 200 else {}
        except BaseException as err: 
            print(err)           
            return {}

    def do_post(self, url="", headers=None, params=None, data=None, json=None):
        url = self.host + url
        try:
            r = self.post(
                url, headers=headers, params=params, data=data, json=json, verify=False
            )            
            return r.json() if r and r.status_code == 200 else {}
        except BaseException as err:  
            print(err)         
            return {}
        
    def has_tasksC(self):
        """获取合成任务"""
        url = "/rebot/postinfer/13/"
        headers = {"Content-Type": "application/json"}
        headers["NAME"] = "aniPortrait"
        headers["VERSION"] = "1.0.0"
        res = self.do_get(url, headers=headers)
        return res 
           
    def post_task_refer(self, guid, uid, code, data, mode="Lipsync", plan="1/1"):
        """提交合成任务"""
        url = "/rebot/postinfer/"
        headers = {"Content-Type": "application/json"}
        headers["NAME"] = "aniPortrait"
        headers["VERSION"] = "1.0.0"
        data = {
            "guid": guid,
            # "user": uid,
            "code": code,
            "data": data,
            "mode": mode,
            "plan": "Lipsync",
        }
        res = self.do_post(url, headers=headers, data=json.dumps(data))       
        return res    
    
    
class syncRequest(requests.Session):
    """syncAPI对象"""

    doc = "https://docs.synclabs.so/api-reference/endpoint/animate/post-animate"
    host = "https://api.synclabs.so"
    key = "1ec49695-1c76-4c14-bd4c-a7527d73ac8a"  # VIP

    def do_get(self, url="", headers=None, params=None, data=None):
        url = self.host + url
        try:
            r = self.get(url, headers=headers, params=params, data=data, verify=False)
            return r.json()  # if r and r.status_code == 200 else {"err": r.text}
        except BaseException as err:
            return {"err": err}

    def do_post(self, url="", headers=None, params=None, data=None, json=None):
        url = self.host + url
        try:
            r = self.post(
                url, headers=headers, params=params, data=data, json=json, verify=False
            )
            return (
                r.json() if r and r.status_code in [200, 201, 403] else {"err": r.text}
            )
        except BaseException as err:
            return {"err": err}

    def hasHeader(self):
        header = {
            "x-api-key": self.key,
            "Content-Type": "application/json",
        }
        return header

    def listLipsync(self, tid=""):
        """lipsync 唇同步"""
        url = f"/lipsync/{tid}"
        header = self.hasHeader()
        res = self.do_get(url, headers=header)
        return res

    def postLipsync(self, audio, video, synergize=True, maxCredits=None):
        """lipsync 唇同步"""
        url = "/lipsync"
        header = self.hasHeader()
        data = {
            "audioUrl": audio,
            "videoUrl": video,
            "synergize": synergize,
            "maxCredits": maxCredits,
            "model": "sync-1.6.0",
            "webhookUrl": "https://lease.3vppt.com/rebot/dosync/",
        }
        res = self.do_post(url, headers=header, json=data)
        return res

    def postAnimate(self, video, text, guid, maxCredits=123):
        """animate 将文本转换为音频，并将其与视频的嘴唇动作同步"""
        url = "/animate"
        header = self.hasHeader()
        data = {
            "videoUrl": video,
            "transcript": text,
            "voiceId": guid,
            "maxCredits": maxCredits,
            "model": "sync-1.6.0",
            "webhookUrl": "https://lease.3vppt.com/rebot/dosync/",
        }
        res = self.do_post(url, headers=header, json=data)
        return res    